<extend name="layout/edit" />

<block name="HEADER">
    <style type="text/css">
         .layui-form-selected dl{
             padding-bottom: 5px;
         }

         .layui-form-select dl{
             max-height: 270px;
         }


    </style>
</block>

<block name="CONTENT">

	<control:form id="form"/>
    
</block>

<block name="FOOTER">
   <script type="text/javascript">
	    var  page=(function(ns){
	    	
	    	//初始化
	    	ns.init = function(){

                form.on("input",'get_money',function(e){
                    //找零=收款金额-(实收-应收)
                    form.setValue('changes',(parseFloat(form.getValue('get_money')).toFixed(2))-(parseFloat(form.getValue('receivable')).toFixed(2)-parseFloat(form.getValue('balancet')).toFixed(2)));
				});

                //选中优惠券切换事件
                form.on("input",'coupon',function(e){
                    if(form.getValue('coupon')==''){
                        form.setValue('coupon_discount','');
                    }

                    $.ajaxs('/admin/v1/order/settlement',{
                        uid:form.getValue('id'),
                        data:window.goodpost,
                        coupon_id:form.getValue('coupon')

                    }).then(function(res){

                        //填充结算信息
                        form.setValue('total_money',res.data.priceGroup.totalSalePrice);    //订单总金额
                        form.setValue('give_balance',res.data.balanceInfo.deductPrice);                 //赠送余额抵扣
                        form.setValue('point_deduct',res.data.integralInfo.deductPrice);                  //积分抵扣

                        //优惠券分两种
                        if(res.data.couponInfo){
                            if(res.data.couponInfo.type=='满折'){
                                form.setValue('coupon_discount',res.data.couponInfo.fracture+'折');        //优惠券优惠折扣
                            }
                            if(res.data.couponInfo.type=='满减'){
                                form.setValue('coupon_discount',res.data.couponInfo.price+'元');             //优惠券优惠元
                            }
                        }

                        //活动优惠分两种
                        if(res.data.activityInfo) {
                            if (res.data.activityInfo.benefit_mode == 1) {
                                form.setValue('activity_discount', res.data.activityInfo.deductPrice + '元');                  //活动满减
                            }
                            if (res.data.activityInfo.benefit_mode == 2) {
                                form.setValue('activity_discount', res.data.activityInfo.benefit_discount + '折');                  //活动满折
                            }
                        }

                        form.setValue('receivable',res.data.priceGroup.payPrice);                        //应收金额
                        form.setValue('receipts',res.data.priceGroup.payPrice);                          //实收金额
                        form.setValue('balancet',res.data.amountInfo.deductPrice);                       //余额支付
                        form.setValue('orderKey',res.data.orderKey);			        	//结算key

                    })

                });

	    	}


            //选择用户
            ns.selectUsers = function(){

                var filter = {
                    url:"modules/common/selectUserCore",
                    mode:1,
                    id:form.getValue('id')
                }
                $.openIframe(filter,"选择会员",['700px', '430px']);

            }

            //用户行 - 删除
            ns.deleteUser = function(){
                form.$grids['userGrid'].deleteRow();
            }

            //选择会员回调函数
            ns.selectUserCoreCallback = function(ids,data){

                //循环动态填充到grid控件
                form.setValue('id',data[0]['uid']);
                form.setValue('name',data[0]['name']);
                form.setValue('level',data[0]['level_name']);
                form.setValue('phone',data[0]['phone']);
                form.setValue('code',data[0]['code']);
                form.setValue('balance',data[0]['balance']);
                form.setValue('integral',data[0]['integral']);
                form.setValue('discounts',data[0]['discounts']);
                form.setValue('type',$.getRequest('type'));

                form.$grids['settlegoodsGrid'].load();       //重新刷新填充商品列表

            }

            //商品增加
            ns.addGoodList =function(){

              if(form.getValue('name')==''){
                   $.toast(0,"需要选择用户才能选择商品!");return;
              }

                var type_name;
                if(form.getValue('type')==1){
                    type_name="普通类开单商品";
                }else if(form.getValue('type')==2){
                    type_name="服务类开单商品";
                }else if(form.getValue('type')==3){
                    type_name="套餐类开单商品";
                }

                var filter = {
                    url:"modules/common/selectBillGoods",
                    mode:1,
                    bill_type:form.getValue('type'),
                    uid:form.getValue('id'),
                    specs_id:ns.getspecsid()

                }
                $.openIframe(filter,type_name,['600px', '480px']);

            }

            //商品截取id
            ns.getspecsid=function(){
                var selectedRows = form.$grids['settlegoodsGrid'].getRows();
                var str = "";
                for(var i=0;i<selectedRows.length;i++){
                    if((selectedRows[i]['id']))
                        str += (selectedRows[i]['id'])+",";
                }
                if (str.length > 0) {
                    str = str.substr(0, str.length - 1);
                }
                return str;
            }

            //选择了商品进行回调
            ns.selectGoodsCallback=function(data){

	    	    //套餐类和服务类（可多个数量）只能选择一次商品
                if(form.getValue('type')==2 || form.getValue('type')==3){
                    form.$grids['settlegoodsGrid'].deleteRow();
                }

                form.$grids['settlegoodsGrid'].addRow(data);

                //将商品规格信息保存在window中
                window.goodpost =$.map(data,function(h){
                    h.id=h.id;
                    h.pay_num=h.pay_num;
                    h.goods_id=h.goods_id;
                    return h;
                });

                //填充所有的商品
                ns.settlementshow(form.$grids['settlegoodsGrid'].getRows());

            }


            //删除控件列表
            ns.deleteRow = function(){
                form.$grids['settlegoodsGrid'].deleteRow();

                //填充所有的商品
                ns.settlementshow(form.$grids['settlegoodsGrid'].getRows());

            }
            //结算页面的填充
            ns.settlementshow=function(data){

	    	    //删除grid商品控件的时候，进行置空
	    	    if(data==''){
                    form.setValue('total_money','');                 //订单总金额
                    form.setValue('give_balance','');                 //赠送余额抵扣
                    form.setValue('point_deduct','');                  //积分抵扣
                    form.setValue('receivable','');                    //应收金额
                    form.setValue('receipts','');                      //实收金额
                    form.setValue('balancet','');                       //余额支付
                    return;
                }
                //调用结算订单进行填充
                $.ajaxs('/admin/v1/order/settlement',{
                    uid:form.getValue('id'),
                    data:data,
					coupon_id:0
                }).then(function(res){

                    //填充结算信息
                    form.setValue('total_money',res.data.priceGroup.totalSalePrice);                 //订单总金额
                    form.setValue('give_balance',res.data.balanceInfo.deductPrice);                 //赠送余额抵扣
                    form.setValue('point_deduct',res.data.integralInfo.deductPrice);                  //积分抵扣
                    form.setValue('receivable',res.data.priceGroup.payPrice);                        //应收金额
                    form.setValue('receipts',res.data.priceGroup.payPrice);                          //实收金额
                    form.setValue('balancet',res.data.amountInfo.deductPrice);                       //余额支付

                    res.data.couponList.unshift({id:'',name:'请选择'});

                    //填充优惠券
                    form.addOption('coupon',$.map( res.data.couponList,function(h,index){

                        h.value = h.id;
                        h.label = h.name;
                        return h;
                    }));

                    //得到结算key
                    form.setValue('orderKey',res.data.orderKey);
                })
            }

	        //确认结算
	        ns.save=function(){

                if (!form.getValue('name')) {
                    $.toast(0,"需要选择用户才能结算!");return;
                }

                if (form.$grids['settlegoodsGrid'].getRows()=='') {
                    $.toast(0,"需要选择商品才能结算!");return;
                }


                //确认结算接口
                $.ajaxs('/admin/v1/order/confirm',{

                    orderKey: form.getValue('orderKey'),
                    mark:form.getValue('remark')}

                    ).then(function(res){

                        if(res.data.status==3){
                            $.toast(1,"已成功开单！")
                        }else{
                            $.toast(0,res.msg);return;
                        }

                    form.opener.grid.load();
                     $.close();

				})

	        }
	        
	        return ns;
	    }({}));
	    
	    
	    $(function(){ page.init() });
   </script>
</block>



